<template>
  <div>
    <van-nav-bar
      :title="`${$route.query.q} 搜索结果`"
      @click-left="$router.back()"
      left-arrow
      fixed
      placeholder
    />
    <!-- 上啦列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id"
        :title="item.title"
        @click="$router.push({ path: '/article', query: { id: item.art_id } })"
      />
    </van-list>
  </div>
</template>

<script>
// 搜索结果接口
import { getSearch } from "./home.js";
export default {
  name: "searchResult",
  data() {
    return {
      // 控制动画的
      loading: false,
      // 是否全部加载完毕
      finished: false,
      // 数组内容
      list: [],
      page: 1,
    };
  },
  methods: {
    // 上拉加载数据
    async onLoad() {
      let res = await getSearch({
        page: this.page,
        q: this.$route.query.q,
      });
      // 绑定数据
      this.list.push(...res.data.data.results);
      this.page++;
      // console.log(res);

      // 关闭动画
      this.loading = false;
      // 判断是否加载完毕
      if (res.data.data.results.length == 0) {
        this.finished = true;
      }
    },
  },
};
</script>

<style>
</style>